import os

from myapp import create_app, db
from flask import request, render_template, jsonify

from myapp import setup_logger

logger = setup_logger(__name__)

app = create_app(os.getenv('FLASK_CONFIG') or 'default')

logger.info('Server started')

@app.errorhandler(400)
def page_not_found(e):
    return jsonify({'msg': '操作失败', 'code': 400, "data": "参数错误！"})

@app.errorhandler(404)
def page_not_found(e):
    return jsonify({'msg': '操作失败', 'code': 404, "data": "找不到此路径！"})

@app.errorhandler(500)
def page_not_found(e):
    return jsonify({'msg': '操作失败', 'code': 500, "data": "系统错误！"})

@app.before_request
def before():
    print("请求地址：" + str(request.path))
    print("请求方法：" + str(request.method))
    print("---请求headers--start--")
    print(str(request.headers).rstrip())
    print("---请求headers--end----")
    if 'GET' == request.method:
        print("GET参数：" + str(request.args))
    else:
        print("POST参数：" + str(request.json))

#     url = request.path  # 当前请求的URL
#     passUrl = ["/captchaImage","/login"]
#     if url in passUrl:
#         pass
# # 测试时注释掉
#     else:
#         return jsonify({'msg': '当前操作没有权限', 'code': 403})
#         _id = session.get("_id", None)
#         if not _id:
#             return jsonify(401, {"msg": "认证失败，无法访问系统资源"})
#         else:
#             pass

if __name__ == '__main__':
    app.run(debug=True)